﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrayParrot.Web.Formly
{
    internal static class InternalHelper
    {
        public static TemplateOptions CreateGrayParrotFileUpload(GrayParrotFileUploadOptionsAttribute grayFileUploadOptionsAttribute)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = grayFileUploadOptionsAttribute.Description,
                Disabled = grayFileUploadOptionsAttribute.Disabled,
                Label = grayFileUploadOptionsAttribute.Label,
                Placeholder = grayFileUploadOptionsAttribute.Placeholder,
                Required = grayFileUploadOptionsAttribute.Required,
                Folder = grayFileUploadOptionsAttribute.Folder,
                Files = grayFileUploadOptionsAttribute.Files,
                Type = "grayParrotFileUpload"
            };

            return retValue;
        }

        //public static TemplateOptions CreateGrayParrotDateTimePicker(string label, string description, string placeHolder, bool disabled,
        //                                 bool required, DateTime? value = null)
        public static TemplateOptions CreateGrayParrotDateTimePicker(GrayParrotDateTimePickerOptionsAttribute grayParrotDateTimePickerOptionsAttribute)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = grayParrotDateTimePickerOptionsAttribute.Description,
                Disabled = grayParrotDateTimePickerOptionsAttribute.Disabled,
                Label = grayParrotDateTimePickerOptionsAttribute.Label,
                Placeholder = grayParrotDateTimePickerOptionsAttribute.Placeholder,
                Required = grayParrotDateTimePickerOptionsAttribute.Required,
                Autoclose = grayParrotDateTimePickerOptionsAttribute.Autoclose,
                Format = grayParrotDateTimePickerOptionsAttribute.Format,
                InitialDate = grayParrotDateTimePickerOptionsAttribute.InitialDate,
                ShowMeridian = grayParrotDateTimePickerOptionsAttribute.ShowMeridian,
                TodayButton = grayParrotDateTimePickerOptionsAttribute.TodayButton,
                TodayHighlight = grayParrotDateTimePickerOptionsAttribute.TodayHighlight,
                Language = grayParrotDateTimePickerOptionsAttribute.Language,
                MinuteStep = grayParrotDateTimePickerOptionsAttribute.MinuteStep,
                WithInitialDate = grayParrotDateTimePickerOptionsAttribute.WithInitialDate,
                ShowTimePanel = grayParrotDateTimePickerOptionsAttribute.ShowTimePanel,
                Type = "grayParrotDateTimePicker"
            };

            retValue.StartView = 2;
            retValue.MinView = 0;

            if (!grayParrotDateTimePickerOptionsAttribute.ShowTimePanel)
            {
                retValue.MinView = 2;
            }

            if (grayParrotDateTimePickerOptionsAttribute.WithInitialDate)
            {
                if (grayParrotDateTimePickerOptionsAttribute.InitialDate != null)
                {
                    retValue.DefaultValue = grayParrotDateTimePickerOptionsAttribute.InitialDate;
                }
            }

            return retValue;
        }

        public static TemplateOptions CreateDateTimePicker(string label, string description, string placeHolder, bool disabled,
                                                 bool required, DataDatetimepickerConfig datetimepickerConfig = null, DateTime? value = null)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = description,
                Disabled = disabled,
                Label = label,
                Placeholder = placeHolder,
                Required = required,
                DataDatetimepickerConfig = datetimepickerConfig,                
                Type = "dateTimePicker"
            };

            if (value != null)
            {
                retValue.DefaultValue = value;
                retValue.Disabled = disabled;
                retValue.Required = required;
            }

            return retValue;
        }

        public static TemplateOptions CreateMaskedInput(string label, string description, string placeHolder, bool disabled,
                                                  bool required, string mask = null)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = description,
                Disabled = disabled,
                Label = label,
                Placeholder = placeHolder,
                Required = required,
                Mask = mask,
                Type = "maskedInput"
            };

            return retValue;
        }

        public static TemplateOptions CreateTimepicker(string label, string description, string placeHolder, bool disabled, bool required)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = description,
                Disabled = disabled,
                Label = label,
                Placeholder = placeHolder,
                Required = required,
                Type = "timepicker"
            };

            return retValue;
        }

        public static TemplateOptions CreateDatepicker(string label, string description, string placeHolder, bool disabled, bool required, string datepickerPopup = null, DateTime? value = null)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = description,
                Disabled = disabled,
                Label = label,
                Placeholder = placeHolder,
                Required = required,
                Type = "datepicker",
                DatepickerPopup = datepickerPopup
            };

            if (value != null)
            {
                retValue.DefaultValue = value;
            }

            return retValue;
        }

        public static TemplateOptions CreateRepeatSection(string label, string description, string placeHolder, bool disabled, bool required, DrawDirections drawDirection)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = description,
                Disabled = disabled,
                Label = label,
                Placeholder = placeHolder,
                Required = required,
                DrawDirection = drawDirection,
                Type = "repeatSection"
            };

            return retValue;
        }

        public static TemplateOptions CreateInput(string label, string description, string placeHolder, bool disabled, bool required, 
                                                  int? minLength = null, int? maxLength = null, TextFieldBehaviors behavior =  TextFieldBehaviors.Text)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = description,
                Disabled = disabled,
                Label = label,
                Placeholder = placeHolder,
                Required = required,
                MinLength = (minLength.HasValue ? minLength.Value : 0),
                MaxLength = (maxLength.HasValue ? maxLength.Value : 0),
                Type = behavior.ToString()
            };

            if (behavior == TextFieldBehaviors.Hidden)
            {
                retValue.Label = string.Empty;
            }

            return retValue;
        }

        public static TemplateOptions CreateNumber(string label, string description, string placeHolder, bool disabled, bool required,
            int? minLength = null, int? maxLength = null, int? min = null, int? max = null, double? value = null)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = description,
                Disabled = disabled,
                Label = label,
                Placeholder = placeHolder,
                Required = required,
                MinLength = minLength.Value,
                MaxLength = maxLength.Value,
                Min = min.Value,
                Max = max.Value,
                Type = "number"
            };

            if (value != double.MinValue)
            {
                retValue.Value = value.Value;
            }
            else
            {
                retValue.Value = null;
            }

            return retValue;
        }

        public static TemplateOptions CreateDecimal(string label, string description, string placeHolder, bool disabled, bool required,
            int? minLength = null, int? maxLength = null, int? min = null, int? max = null, double? value = null, double? step = null)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = description,
                Disabled = disabled,
                Label = label,
                Placeholder = placeHolder,
                Required = required,
                MinLength = minLength.Value,
                MaxLength = maxLength.Value,
                Min = min.Value,
                Max = max.Value,
                Value = value.Value,
                Step = step.Value,
                Type = "number"
            };

            return retValue;
        }

        public static TemplateOptions CreateTextArea(string label, string description, string placeHolder, bool disabled, bool required,
            short rows = 0, short cols = 0)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Label = label,
                Placeholder = placeHolder,
                Description = description,
                Disabled = disabled,
                Required = required,
                Rows = rows,
                Cols = cols
            };

            return retValue;
        }

        public static TemplateOptions CreateCheckBox(string label, bool disabled, bool required, bool isChecked)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Label = label,
                DefaultValue = (isChecked ? true : false),
                Required = required,
                Disabled = disabled
            };

            return retValue;
        }

        public static TemplateOptions CreateMultiCheckBox(string label, string description, string placeHolder, bool disabled, bool required,
            string callback = null, string labelProp = null, string valueProp = null, string dependsBy = null, bool forceLoad = false)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Label = label,
                Placeholder = placeHolder,
                Description = description,
                Disabled = disabled,
                Required = required,
                Callback = callback,
                LabelProp = labelProp,
                ValueProp = valueProp,
                DependsBy = dependsBy,
                ForceLoad = forceLoad
            };

            return retValue;
        }

        public static TemplateOptions CreateRadio(string label, string description, string placeHolder, bool disabled, bool required, string callback = null, object defaultValue = null)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Label = label,
                Placeholder = placeHolder,
                Description = description,
                Disabled = disabled,
                Required = required,
                Callback = callback,
                DefaultValue = defaultValue
            };

            return retValue;
        }

        public static TemplateOptions CreateSelect(string label, string description, string placeHolder, bool disabled, bool required,
            string groupProp = null, string callback = null, string labelProp = null, string valueProp = null, string dependsBy = null, 
            bool forceLoad = false)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Label = label,
                Placeholder = placeHolder,
                Description = description,
                Disabled = disabled,
                Required = required,
                GroupProp = groupProp,
                Callback = callback,
                LabelProp = labelProp,
                ValueProp = valueProp,
                DependsBy = dependsBy,
                ForceLoad = forceLoad
            };
            
            return retValue;
        }

        public static TemplateOptions CreateUISelect2(string label, string description, string placeHolder, bool disabled, bool required,
                   string groupProp = null, string callback = null, string labelProp = null, string valueProp = null, 
                   string dependsBy = null, bool forceLoad = false, uint? minimumInputLength = null, string editProp = null)
        {
            string f = null;
            object m_func = null;

            f = string.Format(@"{0}", callback);
            m_func = new Newtonsoft.Json.Linq.JRaw(f);

            TemplateOptions retValue = new TemplateOptions()
            {
                Label = label,
                Placeholder = placeHolder,
                Description = description,
                Required = required,
                GroupProp = groupProp,
                Callback = callback,
                LabelProp = labelProp,
                ValueProp = valueProp,
                DependsBy = dependsBy,
                ForceLoad = forceLoad,
                NgOptions = "option[to.valueProp] as option in to.options | filter: $select.search",
                OptionsAttr = "bs-options",
                Refresh = "refreshItems",
                //Info = "<div><small>{{option.Descrizione}}</small></div>",
                MinimumInputLength = minimumInputLength.Value,
                EditProp = editProp
            };

            if (disabled)
            {
                retValue.Disabled = disabled;
            }

            return retValue;
        }

        public static TemplateOptions CreateImage(string label, string description, string placeHolder, bool disabled, bool required, string folder)
        {
            TemplateOptions retValue = new TemplateOptions()
            {
                Description = description,
                Disabled = disabled,
                Label = label,
                Placeholder = placeHolder,
                Required = required,
                Folder = folder
            };

            return retValue;
        }
    }
}
